package com.caifeng.goodstwo;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;


public class MyAdapter extends BaseAdapter {
    public final View.OnClickListener listener;
//    private LayoutInflater layoutInflater;
    private List<Goods> list;


//    public MyAdapter(Context context, List<Goods> list,View.OnClickListener listener) {
    public MyAdapter(List<Goods> list,View.OnClickListener listener) {
//        this.layoutInflater = LayoutInflater.from(context);
        this.list = list;
        this.listener=listener;
    }

    @Override
    public int getCount() {
        return list == null ? 1 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    //自定义
    class ViewHolder {
        TextView tname, tnum, tprice;
        ImageView pic, add, minus;
        CheckBox checkBox;

        public ViewHolder(View v) {
            pic = v.findViewById(R.id.pic);
            tname = v.findViewById(R.id.t_name);
            tnum = v.findViewById(R.id.t_num);
            tprice = v.findViewById(R.id.t_price);
            add = v.findViewById(R.id.jia);
            minus = v.findViewById(R.id.jian);
            checkBox=v.findViewById(R.id.box);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;

        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();

        }
        final Goods noteInfo = (Goods) getItem(position);
        viewHolder.tnum.setText(noteInfo.getNum());
        viewHolder.tprice.setText(noteInfo.getPrice());
        viewHolder.tname.setText(noteInfo.getName());
        viewHolder.pic.setImageResource(R.mipmap.youku);
        viewHolder.minus.setOnClickListener(listener);
        viewHolder.add.setOnClickListener(listener);
        viewHolder.add.setTag(position);
        viewHolder.minus.setTag(position);

        return convertView;
    }
}
